/**
 * 页面入口
 **/
var Kindeditor;
window.addEvent('load',function()
{
    //表单按钮事件
	$('ActionButton').addEvent('click', function(e)
	{
		if(CheckForm())
		{
			new Request_Binary(
			{
				//指定表单
				id:'Form1',
				//Ajax请求成功回调
				onSuccess:function($value){Form_Success($value);},
				//AJAX请求发生时回调
				onProcess:Form_Process
			}).send();
		}
		else
		{
			return false;
		}
	});
	//创建编辑器
	Kindeditor=KindEditor.create('#Intro',
    {
        allowFileManager :true
    });
});

// ajax
var xmlHttp = false;
try {
  xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      xmlHttp = false;
    }
  }
}
function callServer() {
  var patrn_email =/^[\w\.\-\_]+@([\w\-\_]+\.)+[a-z]{2,4}$/;
  var username=document.getElementById("username").value;
  	if(username==""){
		$("username_aj").innerHTML="<span style='border:1px solid #e6e2e2;  background:#fff'>  用户名不能为空 </span>";
		return false;
	}
	else{
		if(!patrn_email.test(username)){
			$("username_aj").innerHTML="<span style='border:1px solid #e6e2e2;  background:#fff'> 邮箱格式不正确！</span>";
			return false;
		}else
		{
			$("username_aj").innerHTML="";
		}
	}
  var send_string="username="+username+"";
  send_string= encodeURI(send_string);
  var url = "server.php";
  xmlHttp.open("post", url, true);
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.onreadystatechange = updatePage;
  xmlHttp.send(send_string);
}

function updatePage() {
  if (xmlHttp.readyState == 4) {
	  if(xmlHttp.status==200){
		var response = xmlHttp.responseText;
	   $("username_aj").innerHTML=response;
	  }else{
	  }
  }else{
	 // alert(xmlHttp.readyState);
  }
}

function LinkmanServer(){
  var Linkman=document.getElementById("Linkman").value;
	if(Linkman==""){
		$("Linkman_aj").innerHTML="<span style='border:1px solid #e6e2e2;  background:#fff'> 联系人不能为空 </span>";
		return false;
	}else
	{
		$("Linkman_aj").innerHTML="";
		return true;
	}
}

function DepartmentServer(){
  var Department=document.getElementById("Department").value;
	if(Department==""){
		$("Department_aj").innerHTML="<span style='border:1px solid #e6e2e2;  background:#fff'> 部门不能为空 </span>";
		return false;
	}else
	{
		$("Department_aj").innerHTML="";
		return true;
	}	
}

function PositionServer(){
  var Position=document.getElementById("Position").value;
	if(Position==""){
		$("Position_aj").innerHTML="<span style='border:1px solid #e6e2e2;  background:#fff'> 职位不能为空 </span>";
		return false;
	}else
	{
		$("Position_aj").innerHTML="";
		return true;
	}	
}


function CheckForm()
{
    var Password=$('Password');
    if(Password.value!='')
	{
		
		var Password_again=$('Password_again');
	    if(Password_again.value=='')
		{
			malert('请填写确认密码');
			Password_again.focus();
			return false;
		}
	    if(Password.value!=Password_again.value)
		{
			malert('两次密码输入不一致');
			Password_again.focus();
			return false;
		}
	}
	var Linkman=$('Linkman');
    if(Linkman.value=='')
	{
		malert('请填写联系人');
		Linkman.focus();
		return false;
	}
	var Department=$('Department');
    if(Department.value=='')
	{
		malert('请填写部门');
		Department.focus();
		return false;
	}
	/*var classID=$('ClassID');
    if(classID.value==''||classID.value=='0')
	{
		malert('请选择问题类别');
		classID.focus();
		return false;
	}		
	*/
    return true;
}
/**
 * 函数片断
 **/

//表单AJAX提交后回调函数-成功
function Form_Success($value)
{
    var result=JSON.decode($value);
    switch(result.Number)
    {
        case '0':       
            MUI.App.Process.Update
            ({
                content:'信息修改成功',
                isHaveCloseButton:false,
                onSuccess:function()
                {
                    MUI.App.Process.Close
                    ({
                        onSuccess:function()
                        {
							var Location=$('location');
							if(Location){
                            	window.location=Location.value+'.php?location='+Location.value;
							}else{
								alert('函数错误！！！');	
							}
						}
                    });
                }
            });       
        break;

        default:
            MUI.App.Process.Update
            ({
                content:result.Description,
                isHaveCloseButton:true             
            });
    }
}

//表单数据提交时回调函数
function Form_Process()
{
    MUI.App.Process.Call();
}

jQuery.noConflict();
jQuery(document).ready(function($)
{
	//初始化-省、市
	var provinceValue=$('#ProvinceValue').val();
	var cityValue=$('#CityValue').val();
	try
	{
		$.initProv("#Province","#City",$.DATA_PROVINCE_CITY[provinceValue].n,$.DATA_PROVINCE_CITY[provinceValue].c[cityValue]);
	}
	catch(e){}
	
	//主营行业控件绑定
	var tcv=$('#Trade_Class_Value');
	var tcsv=$('#Trade_Subclass_Value');
	var trade_class=$('#Trade_Class');
	var trade_subclass=$('#Trade_Subclass');
	var trade_result=$('#Trade_Result');
	var button_trade_add=$('#Trade_Add');
	var button_trade_delete=$('#Trade_Delete');
	var function_refresh=function()
	{
		var value_class='';
		var value_subclass='';
		trade_result.children('option').each(function()
		{
			var tmp=$(this).val().split('-');
			value_class=value_class+(tmp[0]+',');					
			value_subclass=value_subclass+(tmp[1]+',');			
		});
			
		value_subclass=value_subclass.substring(0,value_subclass.length-1).split(',');		
		tcsv.val($.unique(value_subclass));		
		
		value_class=value_class.substring(0,value_class.length-1).split(',');
		tcv.val($.unique(value_class));
	};
	
	trade_class.change(function()
	{
		var $this=$(this);
		$.get('/exec.php?action=data_trade',{identifying:$this.val()},function($data)
		{			
			var data=$data.split('|');
			trade_subclass.empty();
			for(var i=0;i<data.length;i++)
			{
				var tmp=data[i].split('-');
				trade_subclass.append('<option value="'+$this.val()+'-'+tmp[1]+'">'+tmp[0]+'</option>');
			}
		});
	});
	
	button_trade_add.click(function()
	{
		if(trade_result.find('option').size()>=6){
			return false;
		}
		if(trade_subclass.val()&&trade_result.find("option[value='"+trade_subclass.val()+"']").val()==null)
		{
			trade_result.append('<option value="'+trade_subclass.val()+'">'+trade_subclass.find("option:selected").text()+'</option>')
		}
		function_refresh();
		return false;
	});
	
	button_trade_delete.click(function()
	{
		if(trade_result.val())
		{
			trade_result.find("option:selected").remove();
		}
		function_refresh();
		return false;
	});
});